use Test::More tests => 41;
#use Test::More qw(no_plan);

use Firepear::Carrot::Lexer;

BEGIN {
use_ok( 'Firepear::Carrot::Pragma::Include' );
}

my $mu = ['[',']',';',"'",'-'];

my $h = Firepear::Carrot::Pragma::Include->new( mu => $mu );

isa_ok ($h, 'Firepear::Carrot::Pragma::Include');

is ($h->{mu}{o}, '\[\[include', 'open');
is ($h->{mu}{c}, '\]\]', 'close');
is ($h->{mu}{r}, '\;\;\;', 'rem');
is ($h->{mu}{s}, '\;', 'separator');
is ($h->{mu}{q}, "\\'", 'quote');
is ($h->{mu}{z}{o}, '\[', 'rawopen');
is ($h->{mu}{z}{c}, '\]', 'rawclose');

######################################################## 000 - Normal operation
is ($h->status, 0, 'normal ops - 0');
is ($h->{type}, 'INCLUDE', 'should be the include');
is ($h->{metatype}, 'PRAGMA', 'PRAGMA metatype');

##################################################################### 'handles'
is ($h->handles('[[random'), 0, 'No, dont we handle random triggers');
is ($h->handles('[[[data'), 0, 'No, we dont handle verbatim chunks');
is ($h->handles('[section'), 0, 'No, we dont handle tags');
is ($h->handles('text'), 0, 'No, we dont handle raw text');
is ($h->handles('bleh]]'), 0, 'No, we dont handle trigger terminals');
is ($h->handles(']]]'), 0, 'No, we dont handle verbatim terminals');
is ($h->handles('bleh]'), 0, 'No, we dont handle tag terminals');
is ($h->handles('[[include'), 1, 'Yes, we handle Include pragmas');

################################################### 001 - Complete node parsing
my $t = "[[include file;./t/corpus/token.1]]";
my $l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
my $node = $h->lex($l);
is ($h->status, 1, 'node processed completely - 1');
is (defined $node, 1);
is (ref $node, 'HASH', 'should be a hashref');
is ($l->current, ']]', 'Lexer in expected state');
is ($node->{type}, 'INCLUDE');
is ($node->{element}, 'include');
is ($node->{refid}, undef);
is ($node->{finfo}[0], 1, 'file number');
is ($node->{finfo}[1], 1, 'line in file');
is (ref $node->{attrs}, 'ARRAY');
is ($node->{attrs}[0], 'file');
is ($node->{attrs}[1], './t/corpus/token.1');

##################################################### 600 - Content not allowed
$t = "[[include file;./t/corpus/token.1 foo bar]]";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
$node = $h->lex($l);
is ($h->status, 600, 'content disallowed - 600');
is ($l->current, 'foo');

################################################### 603 - Schema must be 'file'
$t = "[[include zazz;./t/corpus/token.1]]";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
$node = $h->lex($l);
is ($node->{attrs}[0], 'zazz');
is ($h->status, 603, 'unknown attribute (must be "file") - 603');
is ($l->current, ']]');

######################## 602/604 - One and only one schema/pointer pair allowed
$t = "[[include file;foo file;bar]]";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
$node = $h->lex($l);
is ($h->status, 604, 'too many params - 604');
is ($l->current, ']]');

$t = "[[include ]]";
$l = Firepear::Carrot::Lexer->new( files => [ {scalar => 1, text => $t} ] );
$l->fetch; # prime
$h->status(0);
$node = $h->lex($l);
is ($h->status, 602, 'not enough params - 602');
is ($l->current, ']]');
